package pathways.Database.Specialized.Visual;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import pathways.CCompound;
import pathways.Database.Implementation.CDatabase;

public class CEditCompound extends JDialog
{
  JLabel lblCmpdName = new JLabel();
  JLabel lblECNO = new JLabel();
  JTextField txtCmpd = new JTextField();
  JTextField txtECNO = new JTextField();
  JButton cmdSave = new JButton();
  JButton cmdClose = new JButton();
  
  private CCompound mCmpd = null;
  private CDatabase db = null;
    
  public CEditCompound(CCompound cmpd, CDatabase db, Frame owner)
  {
    super(owner,true);
    
    defaultConstructor();
    
    cmpd.read(cmpd.getID(),db);
    
    mCmpd = cmpd;    
    this.db = db;
    
    this.txtCmpd.setText(cmpd.getName());
    this.txtECNO.setText(cmpd.getECNO());
    
    setSize(360,160);
  }
  
  private CEditCompound()
  {
    defaultConstructor();
  }
  
  private void defaultConstructor()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }    
  }
  
  private void jbInit() throws Exception
  {
    lblCmpdName.setText("Compound:");
    lblCmpdName.setBounds(new Rectangle(7, 18, 83, 22));
    this.getContentPane().setLayout(null);
    lblECNO.setText("ECNO:");
    lblECNO.setBounds(new Rectangle(7, 54, 78, 16));
    txtCmpd.setBounds(new Rectangle(98, 18, 245, 24));
    txtECNO.setBounds(new Rectangle(98, 54, 245, 24));
    cmdSave.setText("Save");
    cmdSave.setBounds(new Rectangle(98, 97, 78, 27));
    cmdSave.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdSave_actionPerformed(e);
      }
    });
    cmdClose.setBounds(new Rectangle(197, 97, 78, 27));
    cmdClose.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdClose_actionPerformed(e);
      }
    });
    cmdClose.setText("Close");
    this.getContentPane().add(lblCmpdName, null);
    this.getContentPane().add(lblECNO, null);
    this.getContentPane().add(txtCmpd, null);
    this.getContentPane().add(txtECNO, null);
    this.getContentPane().add(cmdClose, null);
    this.getContentPane().add(cmdSave, null);
  }

  void cmdSave_actionPerformed(ActionEvent e)
  {
    try {
      db.getConnection().beginTrans();
    } catch (Exception ex) {}
    
    mCmpd.setName(this.txtCmpd.getText());
    mCmpd.setECNO(this.txtECNO.getText());
    
    if (mCmpd.save(db))
    {
      try {
        db.getConnection().commitTrans();
      } catch (Exception ex) {}
      
      JOptionPane.showMessageDialog(this,"Saved successfully.");
      this.dispose(); 
    }
    else
    {
      try {
        db.getConnection().rollbackTrans();
      } catch (Exception ex) {}
    
      JOptionPane.showMessageDialog(this,"Unable to Save.");
    }
  }

  void cmdClose_actionPerformed(ActionEvent e)
  {
    this.dispose();
  }
}